<?php

namespace app\common\model\video;
use think\Model;

class Video extends Model{
    // 表名
    protected $name = 'video';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text'
    ];
    
    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getStatusList()
    {
        return ['1'=>__('Status 1'),'2'=>__('Status 2'),'3'=>__('Status 3')];
    }

    public function getAdList()
    {
        return ['0'=>__('Ad 0'),'1'=>__('Ad 1')];
    }


    public function getVideoTag($id)
    {
        $categoryModel = new \app\common\model\Category();
        $category = $categoryModel->field('id,name nickname')->where(['type'=>'video_tag','pid'=>$id])->select();
        $category = array_column($category,'nickname','id');
        return $category; 
    }


    /**
     * @return \think\model\relation\BelongsTo
     * 关联表商家表
     */
   public function scenic()
   {
       // return $this->belongsTo('Scenic', 'scenic_id', 'id', [], 'LEFT')->setEagerlyType(0);
       return $this->belongsTo('app\common\model\Scenic','scenic_id');
   }

    /***
     * @return \think\model\relation\BelongsTo
     * 关联分类模型
     */
    public function category()
    {
        return $this->belongsTo('app\common\model\Category','cate');
    }

    /***
     * @return \think\model\relation\BelongsTo
     * 关联学校模型
     */
    public function school()
    {
        return $this->belongsTo('app\common\model\School','school_id');
    }

    /***
     * @return \think\model\relation\BelongsTo
     * 关联用户模型
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\User','create_user');
    }


    /***
     * @return \think\model\relation\BelongsTo
     * 关联视频路径
     */
    public function path()
    {
        return $this->belongsTo('VideoPath','video_id');
    }

    /**
     * [getScenicList 景区列表]
     * @param  [type] $store_id [description]
     * @return [type]           [description]
     */
    public function getScenicList()
    {
        $scenicModel = new \app\common\model\Scenic();
        $scenic = $scenicModel->field('id,scenic_name')->where(['status'=>1])->select();
        $array = ['id'=>0,'scenic_name'=>'无'];
        array_unshift($scenic, $array);
        $scenic = array_column($scenic,'scenic_name','id');
        return $scenic;
    }
    
    /**
     * [getSchoolList 学校列表]
     * @return [type] [description]
     */
    public function getSchoolList()
    {
        $schoolModel = new \app\common\model\School();
        $school = $schoolModel->field('id,school_name')->where(['province_name'=>'山东省'])->select();
        $array = ['id'=>0,'school_name'=>'无'];
        array_unshift($school, $array);
        $school = array_column($school,'school_name','id');
        return $school;
    }



}
